package com.nurt.dao.impl;

import com.nurt.dao.OrderDishDao;
import com.nurt.entity.OrderDishEntity;
import com.nurt.entity.OrderEntity;
import com.nurt.utils.DatabaseManager;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

// 步骤4.1：创建订单菜品接口的实体类。
public class OrderDishDaoImpl implements OrderDishDao {

    // 步骤4.2：创建数据库连接。
    private Connection conn;

    // 步骤4.3：创建构造函数，获取数据库连接。
    public OrderDishDaoImpl() {
        try {
            this.conn = DatabaseManager.getInstance().getConnection();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    // 步骤4.4：实现订单菜品接口的方法。
    public List<OrderDishEntity> selectOrderDishes(int orderId) throws SQLException {
        String sql = "SELECT * FROM orderdishes WHERE orderId = " + orderId;
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        List<OrderDishEntity> orderDishes = new ArrayList<>();
        while (rs.next()) {
            OrderDishEntity orderDish = new OrderDishEntity();
            orderDish.setOrderDishId(rs.getInt("orderDishId"));
            orderDish.setOrderId(rs.getInt("orderId"));
            orderDish.setDishId(rs.getInt("dishId"));
            orderDish.setName(rs.getString("name"));
            orderDish.setQuantity(rs.getInt("quantity"));
            orderDish.setPrice(rs.getDouble("price"));
            orderDishes.add(orderDish);
        }
        return orderDishes;
    }

    public List<OrderDishEntity> selectOrderDishes(OrderEntity order) throws SQLException {
        return selectOrderDishes(order.getOrderId());
    }

    // 步骤4.5：实现订单菜品接口的方法。
    public int updateOrderDish(OrderDishEntity orderDish) throws SQLException {
        String sql = "UPDATE orderdishes SET orderId = ?, dishId = ?, name = ?, quantity = ?, price = ? WHERE orderDishId = ?";
        PreparedStatement stmt = conn.prepareStatement(sql);
        stmt.setInt(1, orderDish.getOrderId());
        stmt.setInt(2, orderDish.getDishId());
        stmt.setString(3, orderDish.getName());
        stmt.setInt(4, orderDish.getQuantity());
        stmt.setDouble(5, orderDish.getPrice());
        stmt.setInt(6, orderDish.getOrderDishId());
        return stmt.executeUpdate();
    }

    // 步骤4.6：实现订单菜品接口的方法。
    public int insertOrderDish(OrderDishEntity orderDish) throws SQLException {
        String sql = "INSERT INTO orderdishes (orderId, dishId, name, quantity, price) VALUES (?, ?, ?, ?, ?)";
        PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
        stmt.setInt(1, orderDish.getOrderId());
        stmt.setInt(2, orderDish.getDishId());
        stmt.setString(3, orderDish.getName());
        stmt.setInt(4, orderDish.getQuantity());
        stmt.setDouble(5, orderDish.getPrice());
        stmt.executeUpdate();
        ResultSet rs = stmt.getGeneratedKeys();
        if (rs.next()) {
            return rs.getInt(1);
        } else {
            throw new SQLException("Creating order dish failed, no ID obtained.");
        }
    }

    @Override
    public void deleteOrderDish(int orderId, int dishId) {
        // 根据订单id和菜单id删除菜单
        String sql = "DELETE FROM orderdishes WHERE orderId = ? AND dishId = ?";
        try (PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, orderId);
            stmt.setInt(2, dishId);
            stmt.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void deleteAllOrderDishes(int orderId) {
        // 根据订单id删除所有菜单
        String sql = "DELETE FROM orderdishes WHERE orderId = ?";
        try (PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, orderId);
            stmt.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
